package com.scau.hwadee.examination.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.*;
import com.scau.hwadee.examination.service.CourseManagerService;
import com.scau.hwadee.examination.service.MajorCourseService;
import com.scau.hwadee.examination.service.ProfessionalManagementService;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.utils.UniqueIDGeneratorUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wrh
 * @description
 */
@Controller
@RequestMapping("/Majorcourse")
@Api(tags = "MajorcourseController", description = "专业课程管理")
public class MajorcourseController {
    @Autowired
    private MajorCourseService majorCourseSetService;

    @Autowired
    private ProfessionalManagementService professionalManagementService;

    @Autowired
    CourseManagerService courseManagerService;

    /**
     * wrh
     *
     * @param
     * @return
     */
    @PostMapping("/majorcourse")
    @ApiOperation(value = "增加专业课程")
    //@ApiImplicitParam(name = "majorcourse",value = "专业课程",dataType = "Majorcourse",required = true)
    public ResponseEntity<ResponseVo> addMajorcourse(@RequestBody String params) {
        Majorcourse majorcourse;
        majorcourse = JsonUtils.JSONToObject(params, Majorcourse.class, null);
        majorcourse.setMcId(new UniqueIDGeneratorUtils.
                GlobalUniqueIDGeneratorUtils().generatorId().substring(20));
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.insertMajorcourse(majorcourse);
        if (cnt > 0) vo.setMessage("新增成功");
        else vo.setMessage("新增失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    /**
     * @param
     * @return
     * @author wrh
     */
    @DeleteMapping("/majorcourse")
    @ApiOperation(value = "删除专业课程")
    //@ApiImplicitParam(name = "majorcourse",value = "专业课程",dataType = "Majorcourse",required = true)
    public ResponseEntity<ResponseVo> deleteMajorcourse(@RequestParam("mcId") String mcId) {
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.deleteMajorcourese(mcId);
        if (cnt > 0)
            vo.setMessage("删除成功");
        else vo.setMessage("删除失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("/majorcourse")
    @ApiOperation(value = "修改专业课程")
    //@ApiImplicitParam(name="majorcourse",value = "专业课程",dataType ="Majorcourse",required = true)
    public ResponseEntity<ResponseVo> updateMajorcourse(@RequestBody String params) {
        ResponseVo vo = ResponseVo.instance();
        Majorcourse majorcourse = JsonUtils.JSONToObject(params, Majorcourse.class, null);
        int cnt = majorCourseSetService.updateMajorcourse(majorcourse);
        if (cnt > 0)
            vo.setMessage("修改成功");
        else vo.setMessage("修改失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/allmajorcourseByMajor")
    @ApiOperation(value = "按照专业查询所有专业课程")
    //@ApiImplicitParam(name="majorcourse",value = "专业课程",dataType = "Majorcourse",required =true)
    public ResponseEntity<ResponseVo> getAllMajorcourseByMajor(@RequestParam("mjId") String mjId,
                                                               @RequestParam("pageNum") int pageNum,
                                                               @RequestParam("pageSize") int pageSize) {
        ResponseVo vo = ResponseVo.instance();
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Majorcourse> pageInfo = majorCourseSetService.selectAllMajorcourseByMajor(pageNum, pageSize, mjId);
        List<Majorcourse> list = pageInfo.getList();
        List<Map> listMap = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map map = new HashMap();
            Majorcourse m = list.get(i);
            map.put("mcId", m.getMcId());
            map.put("csId", m.getCsId());
            map.put("mjId", m.getMjId());
            map.put("mvId", m.getMvId());
            map.put("mrId", m.getMrId());
            map.put("csProperties", m.getCsProperties());
            map.put("examType", m.getExamType());
            map.put("mcAssmethod", m.getMcAssmethod());
            map.put("mcRemark", m.getMcRemark());
            map.put("mcType", m.getMcType());
            map.put("csName", courseManagerService.getCourseByCsId(m.getCsId()).getCsName());
            listMap.add(map);
        }
        vo.setData(new PageInfo<>(listMap));
        //vo.getData();
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/majorcourseBymcId")
    @ApiOperation(value = "按照课程编码查询课程")
    public ResponseEntity<ResponseVo> getCourseByCsId(@RequestParam("csId") String csId) {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(courseManagerService.getCourseByCsId(csId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }


    /**
     * 专业选考课程
     *
     * @param
     * @return
     */
    @PostMapping("/courseoptionalrelation")
    @ApiOperation(value = "增加专业选考课程")

    public ResponseEntity<ResponseVo> addCourseoptionalrelation(@RequestBody String params) {
        Courseoptionalrelation courseoptionalrelation;
        courseoptionalrelation = JsonUtils.JSONToObject(params, Courseoptionalrelation.class, null);
        courseoptionalrelation.setCorId(new UniqueIDGeneratorUtils.
                GlobalUniqueIDGeneratorUtils().generatorId().substring(1, 11));
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.addCourseoptionalrelation(courseoptionalrelation);
        if (cnt > 0) vo.setMessage("新增成功");
        else vo.setMessage("新增失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("/courseoptionalrelation")
    @ApiOperation(value = "修改专业选考课程")
    // @ApiImplicitParam(name="majorcourse",value = "专业课程",dataType ="Majorcourse",required = true)
    public ResponseEntity<ResponseVo> updateCourseoptionalrelation(@RequestBody String params) {
        Courseoptionalrelation courseoptionalrelation = JsonUtils.JSONToObject(
                params, Courseoptionalrelation.class, null);
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.updateCourseoptionalrelation(courseoptionalrelation);
        if (cnt > 0) vo.setMessage("修改成功");
        else vo.setMessage("修改失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/courseoptionalrelationByMajor")
    @ApiOperation(value = "按照专业查找选考课程")
    public ResponseEntity<ResponseVo> getCourseoptionalrelationByMajor(@RequestParam("mjId") String mjId) {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getCourseoptionalrelationByMajor(mjId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    /**
     * 专业加考相关功能
     */
    @PostMapping("/addexamcourse")
    @ApiOperation(value = "增加专业加考课程")

    public ResponseEntity<ResponseVo> addAddexamcourse(@RequestBody String params) {
        Addexamcourse addexamcourse;
        addexamcourse = JsonUtils.JSONToObject(params, Addexamcourse.class, null);
        addexamcourse.setAecId(new UniqueIDGeneratorUtils.
                GlobalUniqueIDGeneratorUtils().generatorId().substring(1, 11));
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.addAddexamcourse(addexamcourse);
        if (cnt > 0) vo.setMessage("新增成功");
        else vo.setMessage("新增失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping("/addexamcourse")
    @ApiOperation(value = "修改专业加考课程")
    // @ApiImplicitParam(name="majorcourse",value = "专业课程",dataType ="Majorcourse",required = true)
    public ResponseEntity<ResponseVo> updateAddexamcourse(@RequestBody String params) {
        Addexamcourse addexamcourse = JsonUtils.JSONToObject(
                params, Addexamcourse.class, null);
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.updateAddexamcourse(addexamcourse);
        if (cnt > 0) vo.setMessage("修改成功");
        else vo.setMessage("修改失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/addexamcourse")
    @ApiOperation(value = "按照专业大类查找加考课程")
    public ResponseEntity<ResponseVo> getAddexamcourseByMajor(@RequestParam("aecMajClass1") String aecMajclass1) {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getAddexamcourseByMajorclass(aecMajclass1));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    /**
     * 证书顶替功能
     */
    @PostMapping("/certificatereplacerelation")
    @ApiOperation(value = "增加证书顶替")

    public ResponseEntity<ResponseVo> addCertificatereplacerelation(@RequestBody String params) {
        Certificatereplacerelation certificatereplacerelation;
        certificatereplacerelation = JsonUtils.JSONToObject(params, Certificatereplacerelation.class, null);
        certificatereplacerelation.setCtrId(new UniqueIDGeneratorUtils.
                GlobalUniqueIDGeneratorUtils().generatorId().substring(1, 11));
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.addCertificatereplacerelation(certificatereplacerelation);
        if (cnt > 0) vo.setMessage("新增成功");
        else vo.setMessage("新增失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/certificatereplacerelation")
    @ApiOperation(value = "按名字查找证书顶替")
    public ResponseEntity<ResponseVo> getCertificatereplacerelationByName(@RequestParam("ctName") String ctName) {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getCertificatereplacerelation(ctName));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/allcertificatereplacerelation")
    @ApiOperation(value = "查找所有证书顶替")
    public ResponseEntity<ResponseVo> getAllCertificatereplacerelation() {
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getAllCertificatereplacerelation());
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    /**
     * 课程顶替
     */
    @PostMapping("/coursereplacerelation")
    @ApiOperation(value = "增加课程顶替")

    public ResponseEntity<ResponseVo> addCoursereplacerelation(@RequestBody String params) {
        Coursereplacerelation coursereplacerelation;
        coursereplacerelation = JsonUtils.JSONToObject(params, Coursereplacerelation.class, null);
        coursereplacerelation.setCrrId(new UniqueIDGeneratorUtils.
                GlobalUniqueIDGeneratorUtils().generatorId().substring(1, 11));
        ResponseVo vo = ResponseVo.instance();
        int cnt = majorCourseSetService.addCoursereplacerelation(coursereplacerelation);
        if (cnt > 0) vo.setMessage("新增成功");
        else vo.setMessage("新增失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/coursereplacerelation")
    @ApiOperation(value = "按照专业查找当前版本课程顶替")
    public ResponseEntity<ResponseVo> getCoursereplacerelationByMajor(@RequestParam("mjId") String mjId) {
        String mvId = professionalManagementService.getMajorByPrimaryKey(mjId).getMvId();
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getCoursereplacerelationByMajor(mjId, mvId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping("/lastcoursereplacerelation")
    @ApiOperation(value = "按照专业查找上一版本课程顶替")
    public ResponseEntity<ResponseVo> getLastCoursereplacerelationByMajor(@RequestParam("mjId") String mjId) {
        String mvId = professionalManagementService.getMajorByPrimaryKey(mjId).getMvId();
        ResponseVo vo = ResponseVo.instance();
        vo.setData(majorCourseSetService.getLastCoursereplacerelation(mjId, mvId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }
}
